package com.wang.boot1.compont;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//@Aspect
//@Component
public class AopAdvice {

    private final Logger logger = LoggerFactory.getLogger(AopAdvice.class);


    @Pointcut("execution(* com.wang.boot1.controller.*.*(..))")
    public void test(){}

    @Before("test()")
    public void before(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        logger.info(request.getRequestURI());
        logger.info(request.getMethod());
        logger.info(request.getRemoteAddr());
        System.out.println("之前------");
    }

    @After("test()")
    public void after(){
        System.out.println("之后---------");
    }

    @Around("test()")
    public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
        System.out.println("aop-001");
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        System.out.println("aop-002");
    }
}
